package com.yu1996.gulimall.ware.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.Query;

import com.yu1996.gulimall.ware.dao.PurchaseDetailDao;
import com.yu1996.gulimall.ware.entity.PurchaseDetailEntity;
import com.yu1996.gulimall.ware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;

@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity>
    implements PurchaseDetailService {

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
    String key = (String) params.get("key");
    if (!StringUtils.isEmpty(key)) {
      wrapper.and(
          w -> {
            w.like("sku_id", key).or().like("purchase_id", key);
          });
    }
    String status = (String) params.get("status");
    if (!StringUtils.isEmpty(status)) {
      wrapper.eq("status", status);
    }
    String wareId = (String) params.get("wareId");
    if (!StringUtils.isEmpty(wareId) && !"0".equalsIgnoreCase(wareId)) {
      wrapper.eq("ware_id", wareId);
    }
    IPage<PurchaseDetailEntity> page =
        this.page(new Query<PurchaseDetailEntity>().getPage(params), wrapper);

    return new PageUtils(page);
  }
  /**
   * 根据采购单id查找所有采购项
   *
   * @param id
   * @return
   */
  @Override
  public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
    List<PurchaseDetailEntity> purchase_id = null;
    if (!StringUtils.isEmpty(id)) {
      purchase_id = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
    }
    return purchase_id;
  }
}
